package dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import common.HibernateUtil;
import model.Student;

public class StudentDAO {
	Session session;
	public void update(Student student) {
		session=HibernateUtil.openSession();
		Transaction tx=session.beginTransaction();//提交事务,把数据保存在数据库中
		session.merge(student);//保存使用merge
		tx.commit();
		session.close();		
	}
	
	public Student check(Student student) {
		session=HibernateUtil.openSession();
		String hql="from Student where name=:name and pwd=:pwd";
//		Query query=session.createQuery(hql);
//		方法一  分步得到所需的信息
//		query.setString("name", student.getName());
//		query.setString("pwd", student.getPwd());

//		方法二 得到student中的所有信息
//		query.setProperties("student");
		
		Query query=session.createQuery(hql)
				.setProperties(student);
		
		@SuppressWarnings("unchecked")
		List<Student> list=query.list();
		student=null;
		if(list.size()>0) {
			return list.get(0);
		}
		return student;
	}

	
	public Student get(Student student) {
		session=HibernateUtil.openSession();
		String hql="from Student where id=:id";		
		Query query=session.createQuery(hql)
				.setProperties(student);
		
		@SuppressWarnings("unchecked")
		List<Student> list=query.list();
		student=null;
		if(list.size()>0) {
			return list.get(0);
		}
		return student;
	}

	//判断旧密码
	public boolean checkpwd(Student student) {
		String hql="from Student where id=:id and pwd=:pwd";
		session=HibernateUtil.openSession();
		Query query=session.createQuery(hql)
				.setProperties(student);
		@SuppressWarnings("unchecked")
		List<Student> list=query.list();
		session.close();
		return list.size()>0?true:false;
		
	}
	
	//获取图片的路径
	public String getUrl(Student student) {		
		String hql="select photo from Student where id=:id";
		session=HibernateUtil.openSession();
		Query query=session.createQuery(hql)
				.setProperties(student);		
		@SuppressWarnings("unchecked")
		List<String> list=query.list();
		session.close();
		return list.get(0);
	}
}
